package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Withdraw;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.model.info.WithdrawInfo;
import ysq.xyg.service.WithdrawService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 提现功能
 *
 * @author congcong tao
 * @date 2018/8/29 17:13
 */
@Controller
@RequestMapping("/withdraw")
public class WithdrawController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(WithdrawController.class);
    @Resource
    private WithdrawService withdrawService;

    private Withdraw withdraw;

    @RequestMapping("/listPage")
    public ModelAndView list(){
        ModelAndView view = new ModelAndView("/withdrawList");
        return view;
    }

    @RequestMapping("/adminListPage")
    public ModelAndView adminListPage(){
        ModelAndView view = new ModelAndView("/withdrawAdminList");
        return view;
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Withdraw withdraw, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Withdraw> pageInfo = withdrawService.pageInfo(withdraw, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (Withdraw item : pageInfo.getList()) {
                row=new JSONObject();
                row.put("id", item.getId());
                row.put("userId", item.getUserId());
                row.put("userName", item.getUserName());
                row.put("money", item.getMoney());
                row.put("status", item.getStatus());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/currentJsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject currentJsonList(HttpServletRequest request, Withdraw withdraw, Page page) throws MalformedURLException, UnsupportedEncodingException {
        BaseUserInfo baseUserInfo = acceptCurrentUser(request);
        withdraw.setUserId(baseUserInfo.getUserid());
        PageInfo<Withdraw> pageInfo = withdrawService.pageInfo(withdraw, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (Withdraw item : pageInfo.getList()) {
                row=new JSONObject();
                row.put("id", item.getId());
                row.put("userId", item.getUserId());
                row.put("userName", item.getUserName());
                row.put("money", item.getMoney());
                row.put("status", item.getStatus());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(HttpServletRequest request, String id){
        BaseUserInfo baseUserInfo = acceptCurrentUser(request);
        ModelAndView view = new ModelAndView("/withdrawUpdateOrSave");
        withdraw = withdrawService.selectByPrimaryKey(id);
        view.addObject("withdraw", withdraw);
        view.addObject("baseUser", baseUserInfo);
        return view;
    }

    @RequestMapping(value = "/selectAdminEntity",method = RequestMethod.GET)
    public ModelAndView selectAdminEntity(HttpServletRequest request, String id){
        BaseUserInfo baseUserInfo = acceptCurrentUser(request);
        ModelAndView view = new ModelAndView("/withdrawAdminUpdateOrSave");
        withdraw = withdrawService.selectEntity(id);
        view.addObject("withdraw", withdraw);
        view.addObject("baseUser", baseUserInfo);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(WithdrawInfo withdrawInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(withdrawInfo.getId())) {
            withdraw = new Withdraw();
            String id = BaseUtil.uId();
            withdraw.setId(id);
            withdraw.setUserId(withdrawInfo.getUserId());
            withdraw.setMoney(withdrawInfo.getMoney());
            withdraw.setStatus(withdrawInfo.getStatus());
            withdraw.setCreateTime(new Date());
            int flag = withdrawService.insert(withdraw);
            if(flag <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            withdraw = withdrawService.selectByPrimaryKey(withdrawInfo.getId());
            if (withdrawInfo.getMoney() != 0) {
                withdraw.setMoney(withdrawInfo.getMoney());
            }
            withdraw.setStatus(withdrawInfo.getStatus());
            int id = withdrawService.updateByPrimaryKey(withdraw);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = withdrawService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
